
package com.dsiguero.tfg.percentdroid;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;
import android.view.Menu;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

class GlossaryItem {

    String term;

    String definition;
}

public class GlossaryActivity extends BaseActivity {

    private final static String[] XML_TAGS = { "item", "term", "definition" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.replaceContentLayout(R.layout.activity_glossary, super.CONTENT_LAYOUT_ID);

        XmlPullParserFactory pullParserFactory = null;
        ArrayList<GlossaryItem> glossaryTerms = null;

        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();

            InputStream is = getApplicationContext().getAssets().open("glossary.xml");
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(is, null);

            // Método que parsea el XML
            glossaryTerms = parseXML(parser);

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private ArrayList<GlossaryItem> parseXML(XmlPullParser parser) throws XmlPullParserException,
            IOException {
        ArrayList<GlossaryItem> glossaryItems = null;
        int eventType = parser.getEventType();
        GlossaryItem currentItem = null;

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String name = null;
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    glossaryItems = new ArrayList<GlossaryItem>();
                    break;

                case XmlPullParser.START_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase(XML_TAGS[0]))
                        currentItem = new GlossaryItem();
                    else if (currentItem != null) {
                        if (name.equalsIgnoreCase(XML_TAGS[1]))
                            currentItem.term = parser.nextText();
                        else if (name.equalsIgnoreCase(XML_TAGS[2]))
                            currentItem.definition = parser.nextText();
                    }
                    break;

                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if (name.equalsIgnoreCase(XML_TAGS[0]) && currentItem != null)
                        glossaryItems.add(currentItem);
            }
            eventType = parser.next();
        }
        return glossaryItems;
    }

    // TODO LISTVIEW:
    // http://www.vogella.com/tutorials/AndroidListView/article.html

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }
}
